<?php
declare(strict_types=1);

namespace Service\CommodityService;

use Service\BaseService;
use App\Model\BbcModel\GoodsClass;

/**
 * 渠道商品服务-公共逻辑
 * @author chenshubo
 */
class ChannelCommodityCommonService extends BaseService {
    /**
     * 组装查询条件
     * @param array $param
     */
    public function handleWhere(array $param) {
        $where    = "df=0";
        $is_trade = $param['is_trade'] ?? 0;
        if (isset($param['is_trade'])) {
            $where .= " and is_trade={$param['is_trade']}";
        }
        if ($param['cid'] > 0) {
            $where .= " and cid={$param['cid']}";
        } else {
            $where .= " and cid=0";
        }
        if (!empty($param['word'])) {
            $word  = $param['word'];
            $where .= " and (skucode like \"%{$param['word']}%\" or barcode like \"%{$param['word']}%\" or name like \"%{$param['word']}%\" or origin like \"%{$param['word']}%\")";
        }
        if (!empty($param['class_id'])) {
            $children = GoodsClass::where('pid', $param['class_id'])->pluck('id')->toArray();;
            array_push($children, $param['class_id']);
            $class_ids = implode(',', $children);
            $where     .= " and class_id in ({$class_ids})";
        }
        if (isset($param['status']) && in_array($param['status'], ['1', '0'])) {
            $where .= " and status={$param['status']}";
        }
        if (isset($param['sup_id']) && !empty($param['sup_id'])) {
            $where .= " and supplier_id={$param['sup_id']}";
        }
        if (isset($param['brand_id']) && !empty($param['brand_id'])) {
            $where .= " and brand_id={$param['brand_id']}";
        }
        if (isset($param['is_bonded']) && in_array($param['is_bonded'], ['1', '0', '2', '3'])) {
            $where .= " and is_bonded={$param['is_bonded']}";
        }
        if (isset($param['tax_free']) && in_array($param['tax_free'], ['1', '0'])) {
            $where .= " and tax_free={$param['tax_free']}";
        }
        if (isset($param['hi']) && in_array($param['hi'], ['1', '0'])) {
            $where .= " and is_high_goods={$param['hi']}";
        }
        if (!empty($param['has_storage'])) {
            if ($param['has_storage'] == 1) {
                $where .= " and num_storage>0";
            } else {
                $where .= " and num_storage=0";
            }
        }
        if (isset($param['goods_ids']) && !empty($param['goods_ids'])) {
            if (is_array($param['goods_ids']) && count($param['goods_ids']) > 1) {
                $goodsids_str = implode(',', $param['goods_ids']);
                $where        .= " and id in ({$goodsids_str}) ";
            } else {
                $where .= " and id = {$param['goods_ids']} ";
            }
        }
        if (!empty($param['has_price'])) {
            if ($param['has_price'] == 1) {
                $where .= " and price_store>0";
            } else {
                $where .= " and price_store=0";
            }
        }
        if (!empty($param['sp'])) {
            $where .= " and is_fast_delivery=1";
        } else {
            $where .= " and is_fast_delivery<>1";
        }
        if (!empty($param['up'])) {
            //上架
            if ($param['up'] == 1 && $is_trade == 0) {
                $where .= " and num_storage>0 and price_cost <= price_store and price_recommend>=price_store and price_recommend_vip>=price_store";
            }
        }
        if (isset($param['is_price_wrong']) && in_array($param['is_price_wrong'], ['1', '2'])) {
            if ($param['is_price_wrong'] == 1) {
                $where .= " and (price_recommend_vip > price_recommend or price_recommend < price_store or price_cost > price_store)";
            } else {
                $where .= " and (price_recommend_vip<=price_recommend && price_recommend>=price_store && price_cost <= price_store)";
            }
        }
        if (isset($param['is_detail_wrong']) && in_array($param['is_detail_wrong'], ['1', '2'])) {
            if ($param['is_detail_wrong'] == 1) {
                $where .= " and class_id = 0";
            } else {
                $where .= " and class_id > 0";
            }
        }
        if (!empty($param['time_start'])) {
            $time_start = strtotime($param['time_start']);
            $where      .= " and cd >= $time_start";
        }
        if (!empty($param['time_end'])) {
            $time_end = strtotime($param['time_end']);
            $where    .= " and cd <= $time_end";
        }
        if (!empty($param['has_img'])) {
            if ($param['has_img'] == 1) {
                $where .= " and img <> '' ";
            } else {
                $where .= " and img = '' ";
            }
        }
        if (!empty($param['no_sale'])) {
            $time = time() - 30 * 86400;
            if ($param['no_sale'] == 1) {
                $where .= " and sale_time<$time ";
            } else {
                $where .= " and sale_time>=$time ";
            }
        }
        if (isset($param['is_class']) && in_array($param['is_class'], ['1', '2'])) {
            if ($param['is_class'] == 1) {
                $where .= " and class_id > 0";
            } else {
                $where .= " and class_id = 0";
            }
        }
        if (isset($param['medicinal']) && in_array($param['medicinal'], ['0', '1', '2'])) {
            $medicinal = $param['medicinal'];
            $where     .= " and medicinal=$medicinal";
        }
        if (isset($param['down_status']) && in_array($param['down_status'], ['1', '2', '3', '4'])) {
            switch ($param['down_status']) {
                case 1:
                    $where .= " and status=1";
                    break;
                case 2:
                    $where .= " and status=0 and down_status=1";
                    break;
                case 3:
                    $where .= " and status=0 and down_status=2";
                    break;
                case 4:
                    $where .= " and status=0 and down_status=0";
                    break;
                default:
                    break;
            }
        }

        if (isset($param['isfree_postfee']) && in_array($param['isfree_postfee'], ['1', '2'])) {
            if ($param['isfree_postfee'] == 1) {
                $isfree_postfee = 1;
            } else {
                $isfree_postfee = 0;
            }
            $where .= " and isfree_postfee=$isfree_postfee";
        }

        if (isset($param['supply_tag'])) {
            if (is_array($param['supply_tag']) && !empty($param['supply_tag'])) {
                $supply_tag = $param['supply_tag'];
                $where      .= " and (";
                foreach ($supply_tag as $key => $v) {
                    if ($key != count($supply_tag) - 1) {
                        $where .= " find_in_set($v,supply_tag) or";
                    } else {
                        $where .= " find_in_set($v,supply_tag)";
                    }
                }
                $where .= ")";
            }
        }
        if (isset($param['tag'])) {
            if (is_array($param['tag']) && !empty($param['tag'])) {
                $tag   = $param['tag'];
                $where .= " and (";
                foreach ($tag as $key => $v) {
                    if ($key != count($tag) - 1) {
                        $where .= " find_in_set($v,tag) or";
                    } else {
                        $where .= " find_in_set($v,tag)";
                    }
                }
                $where .= ")";
            }
        }
        if (isset($param['manager_tag'])) {
            if (is_array($param['manager_tag']) && !empty($param['manager_tag'])) {
                $manager_tag = $param['manager_tag'];
                $where       .= " and (";
                foreach ($manager_tag as $key => $v) {
                    if ($key != count($manager_tag) - 1) {
                        $where .= " find_in_set($v,manager_tag) or";
                    } else {
                        $where .= " find_in_set($v,manager_tag)";
                    }
                }
                $where .= ")";
            }
        }
        if (!empty($param['price_type']) && (!empty($param['price_start']) || !empty($param['price_end']))) {
            $string = "";
            switch ($param['price_type']) {
                case 1:
                    $string = "price_cost";
                    break;
                case 2:
                    $string = "price_store";
                    break;
                case 3:
                    $string = "price_recommend";
                    break;
                case 4:
                    $string = "price_recommend_vip";
                    break;
            }
            if (!empty($param['price_start'])) {
                $where .= " and $string >= {$param['price_start']}";
            }
            if (!empty($param['price_end'])) {
                $where .= " and $string <= {$param['price_end']}";
            }
        }
        if (isset($param['transit_template'])) {
            if (is_array($param['transit_template']) && !empty($param['transit_template'])) {
                $express_template = implode(',', $param['transit_template']);
                if (count($param['transit_template']) > 1) {
                    $where .= " and template_id in ( $express_template )";
                } else {
                    $where .= " and template_id =  $express_template ";
                }
            }
        }
        return $where;
    }

    /**
     * 组装排序
     * @param array $param
     */
    public function handleSort(array $param) {
        $sort = "up desc,show_order desc,id desc";
        if (isset($param['sort']) && !empty($param['sort'])) {
            $param['sort'] = (int)$param['sort'];
            switch ($param['sort']) {
                case 1:
                    $sort = "price_cost desc,id desc";
                    break;
                case 2:
                    $sort = "price_cost asc,id desc";
                    break;
                case 3:
                    $sort = "price_store desc,id desc";
                    break;
                case 4:
                    $sort = "price_store asc,id desc";
                    break;
                case 5:
                    $sort = "price_recommend desc,id desc";
                    break;
                case 6:
                    $sort = "price_recommend asc,id desc";
                    break;
                case 7:
                    $sort = "price_recommend_vip desc,id desc";
                    break;
                case 8:
                    $sort = "price_recommend_vip asc,id desc";
                    break;
                case 9:
                    $sort = "num_sale desc,id desc";
                    break;
                case 10:
                    $sort = "num_sale asc,id desc";
                    break;
                case 11:
                    $sort = "month_sale desc,id desc";
                    break;
                case 12:
                    $sort = "month_sale asc,id desc";
                    break;
            }
        }
        return $sort;
    }
}